Ext.define('Era.controller.Main',{
    extend: 'Ext.app.Controller',
    
    statics: {
        
        loadComponents: function() {
            
            // load the bar and menu
            this.mainContainer  = Ext.create('Era.view.MainContainer');
            this.menuWindow     = Ext.create('Era.view.MenuWindow');
           
           // initialize stores
           this.stores = [];
           this.stores['events'] = Ext.create('Era.store.Events');
           this.stores['history'] = Ext.create('Era.store.History');
           
           // timers
           this.eventTimer = new Ext.util.DelayedTask(function(){
               
                if (Era.controller.Main.eventsWindow != null && Era.controller.Main.eventsWindow != 'undefined') {
                    Era.controller.Main.eventsWindow.hide();
                }
           });
           
        },

        showMenuWindow: function() {
            this.menuWindow.menuPanel.removeAll();
            this.menuWindow.show();          
        },
        
        showMenuScreen: function(screen,openWindow) {
            
            this.menuWindow.loadScreen(screen);
            
            if (openWindow == true) {
                
                // hide any previews
                Era.controller.Main.hidePreviewWindow();
                
                // hide any outside grids
                Era.controller.Main.hideEvents(0);
                
                this.menuWindow.show();
            }
            
        },
        
        showPreviewWindow: function(view,record) {

            // destroy previous
            if (this.previewWindow != null && this.previewWindow != 'undefined') {
                this.previewWindow.destroy();
            }
            
            // create new
            this.previewWindow = Ext.create('Era.view.PreviewWindow',{record:record});

            //show preview
            this.previewWindow.show();
            
            // align to grid position
            if (view.getEl().getX() < 905) {
                this.previewWindow.alignTo(view.getEl(),'tl-tr',[5,0]);
            } else {
                this.previewWindow.alignTo(view.getEl(),'tr-tl',[-5,0]);
            }
        },
        
        hidePreviewWindow: function() {
            
            // exists?
            if (this.previewWindow != null && this.previewWindow != 'undefined'){
                this.previewWindow.hide();
            }
            
        },
        
        showIconWindow: function() {
            
            // destroy previous
            if (this.iconWindow != null && this.iconWindow != 'undefined') {
                this.iconWindow.destroy();
            }
            
            // create new
            this.iconWindow = Ext.create('Ext.window.Window',{
                title: 'Icons Gallery',
     
                modal: true,
                draggable: false,
                resizable: false,
                shadow: false,
                items: [
                    {
                        xtype: 'iconscreen'
                    }
                ]
            });

            //show preview
            this.iconWindow.show();
            
            
        },
        
        hideIconWindow: function() {
            if (this.iconWindow != null && this.iconWindow != 'undefined') {
                this.iconWindow.hide();
            }
        },
        
        getStore: function(id) {
            
            // clear filters
            this.stores[id].clearFilter();
            
            // return store
            return this.stores[id];

        },
        
        showEvents: function(component,filters) {
            if (filters == null) {
                filters = {};
            }
            
            // destroy previous
            if (this.eventsWindow != null && this.eventsWindow != 'undefined') {
                this.eventTimer.cancel();
                this.eventsWindow.destroy();
            }

            // get reference to the store
            var store = this.getStore('events');

            //apply filters
            if (filters['priority']) {
                store.filter('priority',true);
            };
            
            if (filters['min']) {
                store.filterBy(function(record,id) { 
                    return (record.get('start_date') > filters['min'] && record.get('start_date') != null);
                });
            };
         
            if (filters['max']) {
                store.filterBy(function(record,id) { 
                    return (record.get('start_date') < filters['max'] && record.get('start_date') != null);
                });
            };
     
            // create new
            this.eventsWindow = Ext.create('Ext.window.Window',{
                width: 300,
                layout: 'fit',
                
                closable: false,
                resizable: false,
                draggable: false,
                border: false,
                
                items: [{
                        xtype: 'managescreen',
                        store: store
                }],
                
                listeners: {
                    render : function(c) {
                         c.getEl().on('mouseover', function(){ this.fireEvent('mouseover'); }, c);
                         c.getEl().on('mouseout', function(){ this.fireEvent('mouseout'); }, c);
                    },
                    mouseover: function() {
                        Era.controller.Main.eventTimer.cancel();
                    },
                    mouseout: function() {
                        Era.controller.Main.hideEvents(500);
                    }
                }
            });

            //show preview
            this.eventsWindow.show();
            
            // align to the group of events
            if (component.getEl().getX() < 905) {
                this.eventsWindow.alignTo(component.getEl(),'bl');
            } else {
                this.eventsWindow.alignTo(component.getEl(),'br',[-300,0]);
            }  
        },
        
        hideEvents: function(seconds) {
            this.eventTimer.delay(seconds);
        }
        
        
        
        
        
    }
    
});